package com.example.homework;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.example.homework.entity.Users;
import com.example.homework.entity.Products;
import com.example.homework.entity.Orders;
import java.util.ArrayList;
import java.util.List;

@RestController  // 使用 @RestController，不会找页面
@RequestMapping("/api")
public class Controller {
 //@GetMapping("/users")     // GET - 获取数据
//@PostMapping("/users")    // POST - 创建数据
//@PutMapping("/users/{id}")   // PUT - 更新数据
//@DeleteMapping("/users/{id}") // DELETE - 删除数据
//@PatchMapping("/users/{id}")  // PATCH - 部分更新

 private List<Users> userList = new ArrayList<>();
 private List<Products> productList = new ArrayList<>();
 private Long nextId = 1L;  // 用于生成ID（新增）

@PostMapping("/users")
public ResponseEntity<Users> createUserAccount(
        @RequestParam String username,
        @RequestParam String password,
        @RequestParam int gender,
        @RequestParam String date_of_birth) {
    Users user=new Users();
    user.setUsername(username);
    user.setPassword(password);
    user.setGender(gender);
    user.setDate_of_birth(date_of_birth);
    user.setUserId(nextId++);//注意

    userList.add(user);

    return ResponseEntity.ok(user);
}

@GetMapping("/users/{id}")
public Users getUserInfo(@RequestParam Long id) {
    //Users user=userList.get(id);//get索引需要int类型
    for (int i = 0; i < userList.size(); i++) {
        Users user = userList.get(i);
        if (user.getUserId().equals(id)) {
            return user;
        }
    }
    return null;//没有找到就返回null
}

@PutMapping("/users/{id}")
public ResponseEntity<Users> updateUser(
        @PathVariable Long id,  // 从URL路径获取ID
        @RequestParam String username,
        @RequestParam String password,
        @RequestParam int gender,
        @RequestParam String date_of_birth) {
    for (int i=0;i<userList.size();i++) {
        Users user=userList.get(i);
        if (user.getUserId().equals(id)) {
            user.setUsername(username);
            user.setPassword(password);
            user.setGender(gender);
            user.setDate_of_birth(date_of_birth);

            return ResponseEntity.ok(user);
        }
    }

    // 如果没找到用户，返回404
    return ResponseEntity.notFound().build();
}

@DeleteMapping("/users/{id}")
    public String deleteUssers(@PathVariable Long id) {
    for (int i = 0; i < userList.size(); i++) {
        Users user=userList.get(i);
        if (user.getUserId().equals(id)) {
            return "deleted successfully";
        }
    }
    return "user not found";

    }


    @PostMapping("/products")
    public ResponseEntity<Products> addProduct(//不能重构一个会有毛病
            @RequestParam String  name,
            @RequestParam double price,
            @RequestParam(required = false, defaultValue = "0") int stock) {

        Products product=new Products();
        product.setName(name);
        product.setPrice(price);
        product.setStock(stock);
        product.setId(nextId++);//注意
        productList.add(product);

        return ResponseEntity.ok(product);
    }

    @GetMapping("products")
    public List<Products> getAllProducts(){
        return productList;
    }

    @GetMapping("/products/{id}")
    public Products getProductInfo(@RequestParam Long id) {
        //Users user=userList.get(id);//get索引需要int类型
        for (int i = 0; i < productList.size(); i++) {
            Products product=productList.get(i);
            if (product.getId().equals(id)) {
                return product;
            }
        }
        return null;//没有找到就返回null
    }

    @PutMapping("/products/{id}")
    public ResponseEntity<Products> updateProduct(
            @PathVariable Long id,  // 从URL路径获取ID
            @RequestParam String  name,
            @RequestParam double price,
            @RequestParam int stock) {
        for (int i=0;i<productList.size();i++) {
            Products product=productList.get(i);
            if (product.getId().equals(id)) {
                product.setName(name);
                product.setPrice(price);
                product.setStock(stock);
                return ResponseEntity.ok(product);
            }
        }
        // 如果没找到用户，返回404
        return ResponseEntity.notFound().build();
    }

    @DeleteMapping("/products/{id}")
    public String deleteproducts(@PathVariable Long id) {
        for (int i = 0; i < productList.size(); i++) {
            Products product=productList.get(i);
            if (product.getId().equals(id)) {
                return "deleted successfully";
            }
        }
        return "product not found";

    }

    @RestController
    @RequestMapping("/orders")
    public class OrderController {

        @PostMapping
        public String createOrder(@RequestParam Long user_id,
                                  @RequestParam Long product_id,
                                  @RequestParam Integer quantity) {
            return "订单创建成功: user_id=" + user_id + ", product_id=" + product_id + ", quantity=" + quantity;
        }

        @GetMapping("/{id}")
        public String getOrderById(@PathVariable Long id) {

            return "订单详情: id=" + id + ", status=1, total_amount=100.00";
        }


        @PutMapping("/{id}")
        public String updateOrderStatus(@PathVariable Long id, @RequestParam Integer status) {

            return "订单状态更新成功: id=" + id + ", new_status=" + status;
        }

        @GetMapping("/users/{userId}/orders")
        public List<String> getUserOrders(@PathVariable Long userId) {
            List<String> orders = new ArrayList<>();
            orders.add("订单1: id=1, product_id=101, status=1");
            orders.add("订单2: id=2, product_id=102, status=0");
            orders.add("订单3: id=3, product_id=103, status=2");
            return orders;
        }
    }

}
